import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.Serializable;
import java.rmi.RemoteException;
import java.util.ArrayList;

import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;

public class ServerApplication extends JFrame implements ActionListener {

	WhiteBoardServer server;

	JTextArea messageArea;
	JScrollPane scroller;
	WhiteBoard drawingSurface;
	
	JPopupMenu menu = new JPopupMenu();
	JButton clearButton = new JButton("Clear");

	public ServerApplication(WhiteBoardServer s) {
		super();
		server = s;
//		FlowLayout l = new FlowLayout();
//		this.setComponentOrientation(ComponentOrientation.)
//		this.setLayout(null);
		
//		setLayout(new GridLayout(2, 1));
		messageArea = new JTextArea();
		messageArea.setEditable(false);
		messageArea.setForeground(Color.GRAY);
		messageArea.append("Server started...");
		drawingSurface = server.getBoard();
		populateMenu();
		drawingSurface.addMouseListener(new MouseAdapter(){
			@Override
			public void mouseReleased(MouseEvent arg0) {
				System.out.println("CLICK!");
				if(arg0.getButton() == MouseEvent.BUTTON3){
					menu.show((Component) arg0.getSource(), arg0.getX(), arg0.getY());
				}
			}
			
		});
		scroller = new JScrollPane(messageArea);
		clearButton.setSize(new Dimension(40, 40));
		clearButton.setActionCommand("CLEAR");
		clearButton.addActionListener(this);
		this.add(clearButton);

		JSplitPane splitPane = new JSplitPane(JSplitPane.VERTICAL_SPLIT,
                drawingSurface, scroller);
		splitPane.setDividerLocation(300);
		JSplitPane splitPane2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, splitPane, clearButton);
		splitPane2.setDividerLocation(400);
		this.add(splitPane2);
		
	}
	
	private void populateMenu(){
		JMenuItem clearItem = new JMenuItem("Clear Board");
		clearItem.setActionCommand("CLEAR");
		clearItem.addActionListener(this);
		menu.add(clearItem);
		
	}

	public void appendMessage(String s){
		messageArea.append("\n"+s);
		messageArea.repaint();
	}
	public static void main(String[] args) throws RemoteException {
		WhiteBoardServer s = new WhiteBoardServer();
		
		ServerApplication sa = new ServerApplication(s);
		s.setSlave(sa);
		// JFrame f = new JFrame(title);
//		sa.setAlwaysOnTop(true);
		sa.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		sa.setSize(650, 500);
		sa.setVisible(true);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getActionCommand() == "CLEAR"){
			server.clear();
		}
		
	}

}
